import request from '../utils/request';
//作用：统一封装所有与后端接口交互的 HTTP 请求逻辑

//基础结构
/*const api = {
    functionName(params) {
        return request({
            url: 'xxx',//请求地址（后端接口路径）
            method: 'get' / 'post',//请求方法（GET / POST）
            data: params // 对于 POST 请求时传递数据
            params: params // 对于 GET请求时传递参数
        });
    }
}*/
const api = {
    userLogin(query) {
        return request({
            url: '/user/login',
            method: 'post',
            data: query
        });
    },
    logout(query) {
        return request({
            url: '/user/logout',
            method: 'post',
            params: query
        });
    },
    signIn(data) {
        return request({
            url: '/user/register',
            method: 'post',
            data: data
        });
    },
    getUserInfo(query) {
        return request({
            url: '/user/info',
            method: 'get',
            params: query
        });
    },
    updateUserPublicInfo(data) {
        return request({
            url: '/user/info',
            method: 'put',
            data: data
        });
    },
    updatePassword(query) {
        return request({
            url: '/user/password',
            method: 'put',
            data: query
        });
    },
    addAddress(data) {
        return request({
            url: '/address/add',
            method: 'post',
            data: data
        });
    },
    getAddress(query) {
        return request({
            url: '/address/info',
            method: 'get',
            params: query
        });
    },
    updateAddress(data) {
        return request({
            url: '/address/update',
            method: 'put',
            data: data
        });
    },
    deleteAddress(data) {
        return request({
            url: '/address/delete',
            method: 'delete',
            data: data
        });
    },
    addIdleItem(data) {
        return request({
            url: '/products/release',
            method: 'post',
            data: data
        });
    },
    getIdleItem(query) {
        return request({
            url: '/idle/info',
            method: 'get',
            params: query
        });
    },
    getAllIdleItem(query) {
        return request({
            url: '/idle/all',
            method: 'get',
            params: query
        });
    },
    // findIdleTiem(query) {
    //     return request({
    //         url: '/idle/find',
    //         method: 'get',
    //         params: query
    //     });
    // },
    findIdleItemByLable(classification,query) {
        return request({
            url: `/products/${classification}/items`,
            method: 'get',
            params: query
        });
    },
    rePublishItem(productID) {
        return request({
            url: `/products/${productID}/republish`,
            method: 'patch'
        });
    },
    addOrder(data) {
        return request({
            url: '/order/add',
            method: 'post',
            data: data
        });
    },
    getOrder(query) {
        return request({
            url: '/order/info',
            method: 'get',
            params: query
        });
    },
    updateOrder(data) {
        return request({
            url: '/order/update',
            method: 'post',
            data: data
        });
    },
    getMyOrder(query) {
        return request({
            url: '/order/my',
            method: 'get',
            params: query
        });
    },
    getMySoldIdle(query) {
        return request({
            url: '/order/my-sold',
            method: 'get',
            params: query
        });
    },
    addOrderAddress(data) {
        return request({
            url: '/order-address/add',
            method: 'post',
            data: data
        });
    },
    updateOrderAddress(data) {
        return request({
            url: '/order-address/update',
            method: 'post',
            data: data
        });
    },
    getOrderAddress(query) {
        return request({
            url: '/order-address/info',
            method: 'get',
            params: query
        });
    },
    addFavorite(productId) {
        return request({
            url: `/products/favorite/${productId}`,
            method: 'post'
        });
    },
    getMyFavorite(query) {
        return request({
            url: '/products/favorite/favorites',
            method: 'get',
            params: query
        });
    },
    deleteFavorite(productId) {
        return request({
            url: `/products/favorite/${productId}`,
            method: 'delete'
        });
    },
    checkFavorite(productId) {
        return request({
            url: '/products/favorite/check',
            method: 'get',
            params: { productId: productId }
        });
    },
    getMessage(query) {
        return request({
            url: '/message/info',
            method: 'get',
            params: query
        });
    },
    getAllIdleMessage(query) {
        return request({
            url: '/message/idle',
            method: 'get',
            params: query
        });
    },
    getAllMyMessage(query) {
        return request({
            url: '/message/my',
            method: 'get',
            params: query
        });
    },
    deleteMessage(query) {
        return request({
            url: '/message/delete',
            method: 'get',
            params: query
        });
    },
    getGoods(data) {
        return request({
            url: '/admin/idleList',
            method: 'get',
            headers: {
                Authorization: `Bearer ${localStorage.getItem("admin_token") || ""}`
            },
            params: data
        });
    },deleteGoods(data) {
        return request({
            url: '/admin/deleteGoods',
            method: 'post',
            headers: {
                Authorization: `Bearer ${localStorage.getItem("admin_token") || ""}`
            },
            data: data // 注意是 `data`，不是 `params`
        });
    },updateGoods(data) {
        return request({
            url: '/admin/updateIdleStatus',
            method: 'put',
            headers: {
                Authorization: `Bearer ${localStorage.getItem("admin_token") || ""}`
            },
            data: data // 通过 JSON 发送请求体
        });
    },

    getOrderList(query) {
        return request({
            url: '/order/orderList',
            method: 'get',
            headers: {
                Authorization: `Bearer ${localStorage.getItem("admin_token") || ""}`
            },
            params: query
        });
    },
    getUserData(query) {
        return request({
            url: '/admin/userList',
            method: 'get',
            headers: {
                Authorization: `Bearer ${localStorage.getItem("admin_token") || ""}`
            },
            params: query
        });
    },
    getUserManage(query) {
        return request({
            url: '/admin/list',
            method: 'get',
            params: query
        });
    },
    updateUserStatus(query){
        return request({
            url: '/admin/updateUserStatus',
            method: 'get',
            params: query
        });
    },
    regAdministrator(data){
        return request({
            url: '/admin/add',
            method: 'post',
            data: data
        });
    },
    adminLogin(query) {
        return request({
            url: '/admin/login',
            method: 'post',
            data: query
        });
    },
    loginOut(query) {
        return request({
            url: '/admin/loginOut',
            method: 'get',
            params: query,
            headers: {
                Authorization: `Bearer ${localStorage.getItem("admin_token") || ""}`
            }
        });
    },
    getAllClassification(query){
        return request({
            url: '/products/classification',
            method: 'get',
            params: query
        });
    },
    downIdleItem(productID){
        return request({
            url:`/products/${productID}/status`,
            method:"patch",
        });
    },
    editItem(productId,data){
        return request({
            url: `/products/${productId}/edit`,
            method:'put',
            data: data
        });
    },
    getProductDetail(productId){
        return request({
            url: `/products/loadItem/${productId}`,
            method: 'get',
        })
    },
    getMyPublishItem() {
        return request({
            url: '/products/myProduct/published',
            method: 'get'
        })
    },
    getMyOffsaleItem(){
        return request({
            url: '/products/myProduct/down',
            method: 'get'
        })
    },
    loadProduct(productId) {
        return request({
            url: `/products/loadItem/${productId}`,
            method: 'get'
        });
    },
    incrementViewCount(productId) {
        return request({
            url: `/products/${productId}/view`,
            method: 'post'
        });
    },
    // 获取用户详细信息
    getUserDetail(userId) {
        const token = localStorage.getItem("token");
        console.log('Token from localStorage:', token);
        const authHeader = token ? `Bearer ${token}` : "";
        console.log('Authorization header:', authHeader);
        
        return request({
            url: `/user/${userId}`,
            method: 'get',
            headers: {
                Authorization: authHeader
            }
        });
    },

    // 订单相关接口
    // 获取订单详情
    getOrderDetail(orderId) {
        return request({
            url: '/order/get',
            method: 'get',
            params: {
                id: orderId
            },
            headers: {
                Authorization: `Bearer ${localStorage.getItem("token") || ""}`
            }
        });
    },

    // 获取我的订单列表
    getMyOrders() {
        return request({
            url: '/order/myOrders',
            method: 'get'
        });
    },

    // 获取我卖出的订单
    getMySoldOrders() {
        return request({
            url: '/order/mySoldOrders',
            method: 'get'
        });
    },

    // 检查商品是否有未完成的订单
    checkProductOrders(productId) {
        return request({
            url: `/order/check-product/${productId}`,
            method: 'get'
        });
    },

    // 支付订单
    payOrder(orderId, payData) {
        return request({
            url: `/order/pay/${orderId}`,
            method: 'post',
            data: payData,
            headers: {
                Authorization: `Bearer ${localStorage.getItem("token") || ""}`
            }
        });
    },

    // 更新订单状态
    updateOrderStatus(statusData) {
        return request({
            url: '/order/update',
            method: 'post',
            data: statusData,
            headers: {
                Authorization: `Bearer ${localStorage.getItem("token") || ""}`
            }
        });
    },

    // 创建订单
    createOrder(orderData) {
        return request({
            url: '/order/create',
            method: 'post',
            data: orderData,
            headers: {
                Authorization: `Bearer ${localStorage.getItem("token") || ""}`
            }
        });
    },

    // 删除订单
    deleteOrder(orderId) {
        return request({
            url: '/order/delete',
            method: 'post',
            data: {
                id: orderId
            },
            headers: {
                Authorization: `Bearer ${localStorage.getItem("token") || ""}`
            }
        });
    },

    getFollowings(data) {
        return request({
            url: '/user/follow/following',
            method: 'get',
            params: data,
            headers: {
                Authorization: `Bearer ${localStorage.getItem("token") || ""}`
            }
        });
    },
    getFollowers(data) {
        return request({
            url: '/user/follow/followers',
            method: 'get',
            params: data,
            headers: {
                Authorization: `Bearer ${localStorage.getItem("token") || ""}`
            }
        });
    },
    unfollowUser(targetId) {
        return request({
            url: `/user/follow/${targetId}`,
            method: 'delete',
            headers: {
                Authorization: `Bearer ${localStorage.getItem("token") || ""}`
            }
        });
    },
    followUser(targetId) {
        return request({
            url: `/user/follow/${targetId}`,
            method: 'post',
            headers: {
                Authorization: `Bearer ${localStorage.getItem("token") || ""}`
            }
        });
    },
    // 检查是否关注了某个用户
    checkFollow(targetId) {
        return request({
            url: `/user/follow/check/${targetId}`,
            method: 'get',
            headers: {
                Authorization: `Bearer ${localStorage.getItem("token") || ""}`
            }
        });
    },
    // 添加模糊搜索API
    fuzzySearch(query) {
        return request({
            url: '/products/search',
            method: 'get',
            params: query
        });
    },
    resetUserPassword(data) {
        return request({
            url: '/admin/resetUserPassword',
            method: 'post',
            headers: {
                Authorization: `Bearer ${localStorage.getItem("admin_token") || ""}`
            },
            data: data
        });
    },

    // ========== 骑手相关接口 ==========

    // 骑手注册
    riderRegister(data) {
        return request({
            url: '/rider/register',
            method: 'post',
            data: data
        });
    },

    // 骑手登录
    riderLogin(data) {
        return request({
            url: '/rider/login',
            method: 'post',
            data: data
        });
    },

    // 获取所有待接单的订单
    getAvailableOrders() {
        return request({
            url: '/rider/orders',
            method: 'get',
            headers: {
                Authorization: `Bearer ${localStorage.getItem("rider_token") || ""}`
            }
        });
    },

    // 获取骑手自己接的所有订单
    getRiderMyOrders() {
        return request({
            url: '/rider/my-orders',
            method: 'get',
            headers: {
                Authorization: `Bearer ${localStorage.getItem("rider_token") || ""}`
            }
        });
    },

    // 骑手接单
    acceptOrder(orderId) {
        return request({
            url: `/rider/accept/${orderId}`,
            method: 'put',
            headers: {
                Authorization: `Bearer ${localStorage.getItem("rider_token") || ""}`
            }
        });
    },

    // 骑手完成订单
    completeOrder(orderId) {
        return request({
            url: `/rider/complete/${orderId}`,
            method: 'put',
            headers: {
                Authorization: `Bearer ${localStorage.getItem("rider_token") || ""}`
            }
        });
    },

    // 创建聊天会话
    startChat(data) {
        return request({
            url: '/chats',
            method: 'post',
            data: data,
            headers: {
                Authorization: `Bearer ${localStorage.getItem("token") || ""}`
            }
        });
    },
    // 获取聊天记录
    getChatHistory(chatId) {
        return request({
            url: `/chats/${chatId}/messages`,
            method: 'get',
            headers: {
                Authorization: `Bearer ${localStorage.getItem("token") || ""}`
            }
        });
    },
    // 获取用户的所有聊天会话
    getUserChats() {
        return request({
            url: '/chats/user',
            method: 'get',
            headers: {
                Authorization: `Bearer ${localStorage.getItem("token") || ""}`
            }
        });
    },
    // 发送消息
    sendMessage(chatId, data) {
        return request({
            url: `/chats/${chatId}/messages`,
            method: 'post',
            data: data,
            headers: {
                Authorization: `Bearer ${localStorage.getItem("token") || ""}`
            }
        });
    },
    // 结束聊天
    endChat(chatId) {
        return request({
            url: `/chats/${chatId}/end`,
            method: 'post',
            headers: {
                Authorization: `Bearer ${localStorage.getItem("token") || ""}`
            }
        });
    }
};


export default api;